package com.whmall.service.crm.module.companycontact.impl;

import com.whmall.component.common.ObjectConverter;
import com.whmall.service.crm.entity.dto.companycontact.CreateOrUpdateCompanyContactReqDTO;
import com.whmall.service.crm.module.companycontact.CompanyContactService;
import com.whmall.service.crm.module.companycontact.entity.CompanyContactDO;
import com.whmall.service.crm.module.companycontact.impl.dao.CompanyContactMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * CompanyContactServiceImpl
 *
 * @author alex
 * @since 2020/8/5
 */
@Service
public class CompanyContactServiceImpl implements CompanyContactService {
    private final CompanyContactMapper companyContactMapper;
    private final ObjectConverter<CreateOrUpdateCompanyContactReqDTO, CompanyContactDO> converter;
    private final CreateOrUpdateCompanyContactReqDTOValidator validator;

    public CompanyContactServiceImpl(CompanyContactMapper companyContactMapper,
                                     ObjectConverter<CreateOrUpdateCompanyContactReqDTO, CompanyContactDO> converter,
                                     CreateOrUpdateCompanyContactReqDTOValidator validator) {
        this.companyContactMapper = companyContactMapper;
        this.converter = converter;
        this.validator = validator;
    }

    @Transactional
    @Override
    public void createCompanyContact(Long requesterId, Long companyId, CreateOrUpdateCompanyContactReqDTO request) {
        Assert.notNull(requesterId, "请求员工ID不能为null");
        Assert.notNull(requesterId, "公司ID不能为null");

        //
        // 校验
        validator.validate(request);

        //
        // 创建
        CompanyContactDO companyContact = converter.convert(request);
        companyContact.setCompanyId(companyId);

        //
        // 创建人、创建时间
        companyContact.setCreateId(requesterId);
        companyContact.setCreateTime(new Date());

        //
        // 插入
        companyContactMapper.insertSelective(companyContact);
    }

    @Transactional
    @Override
    public void updateCompanyContact(Long requesterId, Long companyId, Long companyContactId, CreateOrUpdateCompanyContactReqDTO request) {
        Assert.notNull(requesterId, "请求员工ID不能为null");
        Assert.notNull(requesterId, "公司ID不能为null");
        Assert.notNull(companyContactId, "公司联系人ID不能为null");

        //
        // 校验
        validator.validate(request, true);

        //
        // 更新信息
        CompanyContactDO companyContact = converter.convert(request);
        companyContact.setId(companyContactId);

        //
        // 更新人、更新时间
        companyContact.setUpdateId(requesterId);
        companyContact.setUpdateTime(new Date());

        //
        // 插入
        companyContactMapper.updateByIdSelective(companyContact);
    }

    @Transactional
    @Override
    public void removeCompanyContact(Long requesterId, Long companyId, Long companyContactId) {
        Assert.notNull(requesterId, "请求员工ID不能为null");
        Assert.notNull(requesterId, "公司ID不能为null");
        Assert.notNull(companyContactId, "公司联系人ID不能为null");

        Optional.ofNullable(companyContactMapper.getById(companyContactId))
                .filter(e -> Objects.equals(e.getCompanyId(), companyId))
                .filter(e -> Objects.equals(e.getDeleted(), 0))
                .orElseThrow(() -> new IllegalArgumentException("公司联系人不存在"));

        CompanyContactDO update = new CompanyContactDO();
        update.setId(companyContactId);
        update.setDeleted((int) (System.currentTimeMillis() / 1000));

        //
        // 更新人、更新时间
        update.setUpdateId(requesterId);
        update.setUpdateTime(new Date());

        //
        // 软删除
        companyContactMapper.updateByIdSelective(update);
    }

    @Override
    public CompanyContactDO getByCompanyIdAndCompanyContactId(Long companyId, Long companyContactId) {
        return Optional.ofNullable(companyContactMapper.getById(companyContactId))
                .filter(e -> Objects.equals(e.getCompanyId(), companyId))
                .orElse(null);
    }

    @Override
    public List<CompanyContactDO> listByCompanyId(Long companyId) {
        Assert.notNull(companyId, "公司ID不能为null");
        return companyContactMapper.listByCompanyId(companyId);
    }
}
